package com.linkage.seapeak.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.pojo.MultiMessage;
import com.linkage.seapeak.utils.CommonUtils;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class ItemHistoryAdapter extends BaseRecyclerAdapter<ItemHistoryAdapter.ViewHolder> {

    Context context;
    List<MultiMessage> multiMessageList;


    public ItemHistoryAdapter(Context context, List<MultiMessage> multiMessageList) {
        this.context = context;
        this.multiMessageList = multiMessageList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_multi_message, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        MultiMessage item = multiMessageList.get(position);
        if (item != null) {
            List<CrediterUser> users = item.getUsers();
            addAvatar(holder.avatarParent, users);
            holder.desc.setText(item.getOpAt() + " 发送人：" + item.getSendUserName());
            holder.memberSum.setText(item.getUserCount() + "人");
            holder.msgContent.setText(item.getNote());
        }
    }

    /**
     * 添加头像
     * @param avatarParent
     * @param users
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void addAvatar(FrameLayout avatarParent, List<CrediterUser> users) {
        avatarParent.removeAllViews();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                CommonUtils.dp2px(context, 34), CommonUtils.dp2px(context, 34));

        if (users != null && users.size() > 0) {
            for (int i = 0;i < users.size();i++) {
                ImageView child = new ImageView(context);
                Glide.with(context).load(users.get(i).getAvatar())
                        .bitmapTransform(new CropCircleTransformation(context))
                        .placeholder(R.drawable.logo)
                        .into(child);
                FrameLayout.LayoutParams itemLp = new FrameLayout.LayoutParams(params);
                itemLp.leftMargin = CommonUtils.dp2px(context, 28) * i;
                avatarParent.addView(child, itemLp);
                if (i == 7)
                    return;
            }
        }

        avatarParent.invalidate();
    }

    @Override
    public int getItemCount() {
        return multiMessageList.size();
    }


    /**
     * 群发历史消息展示
     */
    class ViewHolder extends BaseHolder {

        private TextView desc;
        private FrameLayout avatarParent;
        private TextView memberSum;
        private TextView msgContent;

        public ViewHolder(View view) {
            super(view);
            desc = (TextView) view.findViewById(R.id.desc);
            avatarParent = (FrameLayout) view.findViewById(R.id.avatar_parent);
            memberSum = (TextView) view.findViewById(R.id.member_sum);
            msgContent = (TextView) view.findViewById(R.id.msg_content);
        }
    }
}
